package com.yyfax.ph.home.imageupload;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.yyfax.ph.R;
import com.yyfax.ph.base.adapter.BaseAdapter;
import com.yyfax.ph.common.imageselector.bean.ImageBean;

import java.util.ArrayList;
import java.util.List;

public class ImageAdapter extends BaseAdapter<ImageBean> {
	public interface ChooseUploadListener {
		void onAddClick();

		void onDelClick(String path);

		void onReClickUpload(String path);

		void onReviewClick(int position);
	}

	private ChooseUploadListener mChooseUploadListener;
	private RequestManager mRequestManager;
	private boolean mShowIndexText = false;

	public ImageAdapter(Context context, ChooseUploadListener chooseUploadListener) {
		super();
		this.mChooseUploadListener = chooseUploadListener;
		mRequestManager = Glide.with(context);
	}

	public ImageAdapter(Context context, ChooseUploadListener chooseUploadListener, boolean showIndexText) {
		this(context, chooseUploadListener);
		mShowIndexText = showIndexText;
	}

	@Override
	public int getCount() {
		return super.getCount() + 1;
	}

	@Override
	public ImageBean getItem(int position) {
		if (position < (getCount() - 1)) {
			return super.getItem(position);
		} else {
			return null;
		}
	}

	public List<String> getSuccessPathList() {
		List<String> successList = new ArrayList<>();
		for (ImageBean bean : getDataSet()) {
			if (ImageBean.STATUS_SUCCESS == bean.status) {
				successList.add(bean.newPath);
			}
		}
		return successList;
	}

	public List<ImageBean> getSuccessBeanList() {
		List<ImageBean> successList = new ArrayList<>();
		for (ImageBean bean : getDataSet()) {
			if (ImageBean.STATUS_SUCCESS == bean.status) {
				successList.add(bean);
			}
		}
		return successList;
	}

	public List<String> getSuccessImageIdList() {
		List<String> successList = new ArrayList<>();
		for (ImageBean bean : getDataSet()) {
			if (ImageBean.STATUS_SUCCESS == bean.status) {
				successList.add(bean.imageId);
			}
		}
		return successList;
	}

	public List<String> getFailPathList() {
		List<String> failList = new ArrayList<>();
		for (ImageBean bean : getDataSet()) {
			if (ImageBean.STATUS_FAIL == bean.status) {
				failList.add(bean.newPath);
			}
		}
		return failList;
	}

	public List<ImageBean> getFailBeanList() {
		List<ImageBean> failList = new ArrayList<>();
		for (ImageBean bean : getDataSet()) {
			if (ImageBean.STATUS_FAIL == bean.status) {
				failList.add(bean);
			}
		}
		return failList;
	}

	public List<String> getPathList() {
		List<String> failList = new ArrayList<>();
		for (ImageBean bean : getDataSet()) {
			failList.add(bean.newPath);
		}
		return failList;
	}

	public ArrayList<String> getOriginalPathList() {
		ArrayList<String> failList = new ArrayList<>();
		for (ImageBean bean : getDataSet()) {
			failList.add(bean.originalPath);
		}
		return failList;
	}

	public void changeStatus(String filePath, int status, String imageId) {
		ImageBean target = new ImageBean(filePath, filePath);
		int size = getDataSet().size();
		for (int i = 0; i < size; i++) {
			if (target.equals(getItem(i))) {
				getDataSet().get(i).status = status;
				getDataSet().get(i).imageId = imageId;
				return;
			}
		}
	}

	public boolean isUploading(){
		int size = getDataSet().size();
		for (int i = 0; i < size; i++) {
			if (ImageBean.STATUS_ING == getItem(i).status) {
				return true;
			}
		}
		return false;
	}

	public List<String> getNeedUploadPath() {
		List<String> failList = new ArrayList<>();
		int size = getDataSet().size();
		for (int i = 0; i < size; i++) {
			if (ImageBean.STATUS_SUCCESS != getItem(i).status) {
				getDataSet().get(i).status = ImageBean.STATUS_ING;
				failList.add(getDataSet().get(i).newPath);
			}
		}
		return failList;
	}

	private void showAdd(ViewHolder viewHolder) {
		viewHolder.mUploadImgAddIv.setVisibility(View.VISIBLE);
		viewHolder.mUploadImgIv.setVisibility(View.GONE);
		viewHolder.mUploadImgStatusTv.setVisibility(View.GONE);
		viewHolder.mUploadImgDelIv.setVisibility(View.GONE);
		viewHolder.mPicIndexTv.setVisibility(View.GONE);
	}

	private void showSuccess(ViewHolder viewHolder) {
		viewHolder.mUploadImgAddIv.setVisibility(View.GONE);
		viewHolder.mUploadImgStatusTv.setVisibility(View.GONE);
		viewHolder.mUploadImgIv.setVisibility(View.VISIBLE);
		viewHolder.mUploadImgDelIv.setVisibility(View.VISIBLE);
	}

	private void showFailure(Context context, ViewHolder viewHolder) {
		viewHolder.mUploadImgAddIv.setVisibility(View.GONE);
		viewHolder.mUploadImgStatusTv.setText(context.getString(R.string.upload_image_failure));
		viewHolder.mUploadImgStatusTv.setVisibility(View.VISIBLE);
		viewHolder.mUploadImgIv.setVisibility(View.VISIBLE);
		viewHolder.mUploadImgDelIv.setVisibility(View.VISIBLE);
	}

	private void showUploading(Context context, ViewHolder viewHolder) {
		viewHolder.mUploadImgAddIv.setVisibility(View.GONE);
		viewHolder.mUploadImgDelIv.setVisibility(View.GONE);
		viewHolder.mUploadImgIv.setVisibility(View.VISIBLE);
		viewHolder.mUploadImgStatusTv.setText(context.getString(R.string.uploading));
		viewHolder.mUploadImgStatusTv.setVisibility(View.VISIBLE);

	}

	private void showPicIndex(Context context, ViewHolder viewHolder, int position) {
		if (mShowIndexText) {
			viewHolder.mPicIndexTv.setVisibility(View.VISIBLE);
			viewHolder.mPicIndexTv.setVisibility(View.VISIBLE);
			viewHolder.mPicIndexTv.setText(String.format(context.getString(R.string.index_credit_paper), String.valueOf(position + 1)));
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		Context context = parent.getContext();
		if (convertView == null) {
			convertView = View.inflate(parent.getContext(), R.layout.upload_credit_grid_view_item_layout, null);
			viewHolder = new ViewHolder(convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (mDataSet.size() <= 0 || position == (getCount() - 1)) {
			viewHolder.mUploadImgAddItem.setBackgroundResource(0);
			showAdd(viewHolder);
		} else {
			showPicIndex(context, viewHolder, position);
			ImageBean bean = getItem(position);
//			viewHolder.mUploadImgAddItem.setBackgroundResource(R.drawable.credit_gridview_item_bg_shape);
			switch (bean.status) {
				case ImageBean.STATUS_SUCCESS:
					showSuccess(viewHolder);
					break;
				case ImageBean.STATUS_FAIL:
					showFailure(context, viewHolder);
					break;
				case ImageBean.STATUS_ING:
					showUploading(context, viewHolder);
					break;
				default:
					showUploading(context,viewHolder);
					break;
			}
			mRequestManager.load(bean.newPath).placeholder(R.drawable.mis_skin_icon_default_error)
					.error(R.drawable.mis_skin_icon_default_error).centerCrop().into(viewHolder.mUploadImgIv);
		}

		viewHolder.mUploadImgAddItem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (null != mChooseUploadListener) {
					if (position == (getCount() - 1)) {
						mChooseUploadListener.onAddClick();
					} else {
						ImageBean bean = getItem(position);
						switch (bean.status) {
							case ImageBean.STATUS_SUCCESS:
								mChooseUploadListener.onReviewClick(position);
								break;
							case ImageBean.STATUS_FAIL:
								mChooseUploadListener.onReClickUpload(bean.newPath);
								break;
						}
					}
				}
			}
		});

		viewHolder.mUploadImgDelIv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (null != mChooseUploadListener) {
					ImageBean bean = getItem(position);
					mChooseUploadListener.onDelClick(bean.newPath);
					//delImage(imagePath);
				}
			}
		});
		return convertView;
	}


	static class ViewHolder {
		@BindView(R.id.upload_img_add_iv)
		ImageView mUploadImgAddIv;
		@BindView(R.id.upload_img_iv)
		ImageView mUploadImgIv;
		@BindView(R.id.upload_img_status_tv)
		TextView mUploadImgStatusTv;
		@BindView(R.id.pic_index_tv)
		TextView mPicIndexTv;
		@BindView(R.id.upload_img_del_iv)
		ImageView mUploadImgDelIv;
		@BindView(R.id.upload_img_add_item)
		LinearLayout mUploadImgAddItem;

		ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}
}
